﻿using CommunityToolkit.Mvvm.Messaging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CommunityToolkitDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //消息注册
            RegisterMessage();
        }

        private void RegisterMessage()
        {
            WeakReferenceMessenger.Default.Register<string>(this,HandleMessage);
        }

        private void HandleMessage(object recipient, string message)
        {
            switch (message)
            {
                case "MainWindowClose":
                    AppClose();
                    break;
                case "MainWindowMax":
                    MainWindowMax();
                    break;
                case "MainWindowMin":
                    MainWindowMin();
                    break;
            }
        }

        private void DragMoveWindow(object sender, MouseButtonEventArgs e)
        {
            this.WindowState = WindowState.Normal;
            if (e.LeftButton == MouseButtonState.Pressed && this.WindowState != WindowState.Maximized)
            {
                this.DragMove();
            }
        }

        /// <summary>
        /// 窗体最小化
        /// </summary>
        private void MainWindowMin()
        {
            this.WindowState = WindowState.Minimized;
        }
        /// <summary>
        /// 窗体最大化
        /// </summary>
        private void MainWindowMax()
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.WindowState = WindowState.Normal;
            }
            else
            {
                this.WindowState = WindowState.Maximized;
            }
        }
        /// <summary>
        /// 退出系统
        /// </summary>
        private void AppClose()
        {
            //DataCenter.Instance.Dispose();
            string appName = AppDomain.CurrentDomain.SetupInformation.ApplicationName.Replace(".exe", "");
            foreach (var item in Process.GetProcessesByName(appName))
            {
                item.Kill();
            }
        }

    }
}
